<template>
  <view class="brand-detail-container">
    <!-- 内容区 -->
    <view class="content-section" v-if="!loading && !error">
      <!-- 基本信息 -->
      <view class="basic-info">
        <text class="detail-title">{{ tableInfo.name }}</text>
        <text class="detail-subtitle">编号: {{ tableInfo.serial_no }}</text>
      </view>
      
      <!-- 详细信息列表 -->
      <view class="detail-list">
        <view class="detail-item">
          <text class="detail-label">虚拟台桌:</text>
          <text class="detail-value">{{ tableInfo.is_virtual === '1' ? '是' : '否' }}</text>
        </view>
        <view class="detail-item">
          <text class="detail-label">台桌类型ID:</text>
          <text class="detail-value">{{ tableInfo.type_id || '-' }}</text>
        </view>
        <view class="detail-item">
          <text class="detail-label">品牌ID:</text>
          <text class="detail-value">{{ tableInfo.brand_id || '-' }}</text>
        </view>
        <view class="detail-item">
          <text class="detail-label">收费类型:</text>
          <text class="detail-value">{{ tableInfo.fee_type === 'hour' ? '按小时' : '其他' }}</text>
        </view>
        <view class="detail-item">
          <text class="detail-label">计费时长:</text>
          <text class="detail-value">{{ tableInfo.duration || 0 }}{{ tableInfo.fee_type === 'hour' ? '小时' : '' }}</text>
        </view>
        <view class="detail-item">
          <text class="detail-label">价格:</text>
          <text class="detail-value">¥{{ tableInfo.price || '0.00' }}</text>
        </view>
        <view class="detail-item">
          <text class="detail-label">VIP价格:</text>
          <text class="detail-value">¥{{ tableInfo.price_vip || '0.00' }}</text>
        </view>
        <view class="detail-item">
          <text class="detail-label">VIP价格类型:</text>
          <text class="detail-value">{{ tableInfo.price_vip_type === 'custom' ? '自定义' : tableInfo.price_vip_type || '-' }}</text>
        </view>
        <view class="detail-item">
          <text class="detail-label">是否分钟计费:</text>
          <text class="detail-value">{{ tableInfo.is_minute === 1 ? '是' : '否' }}</text>
        </view>
        <view class="detail-item">
          <text class="detail-label">状态:</text>
          <text class="detail-value">{{ tableInfo.status === '1' ? '启用' : '禁用' }}</text>
        </view>
        <view class="detail-item">
          <text class="detail-label">免费时长:</text>
          <text class="detail-value">{{ tableInfo.free_duration || 0 }}{{ tableInfo.free_type === 'first' ? ' (首次)' : '' }}</text>
        </view>
        <view class="detail-item">
          <text class="detail-label">创建时间:</text>
          <text class="detail-value">{{ formatDate(tableInfo.createtime) }}</text>
        </view>
        <view class="detail-item">
          <text class="detail-label">更新时间:</text>
          <text class="detail-value">{{ formatDate(tableInfo.updatetime) }}</text>
        </view>
      </view>
      
      <!-- 操作按钮 -->
      <view class="action-buttons">
        <button class="edit-btn" type="default" @click="handleEdit">编辑</button>
        <button class="delete-btn" type="warn" @click="handleDelete">删除</button>
      </view>
    </view>
    
    <!-- 加载状态 -->
    <view class="loading-state" v-if="loading">
      <view class="loading-spinner"></view>
      <text class="loading-text">加载中...</text>
    </view>
    
    <!-- 错误状态 -->
    <view class="error-state" v-if="error">
      <image src="/static/error.png" mode="aspectFit" class="error-icon"></image>
      <text class="error-text">{{ errorMessage }}</text>
      <button class="retry-btn" @click="getTableDetail">重试</button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      tableInfo: {},
      loading: false,
      error: false,
      errorMessage: ''
    };
  },
  onLoad(options) {
    if (options.id) {
      this.getTableDetail(options.id);
    }
  },
  methods: {
    // 获取台桌详情
    async getTableDetail(id) {
      try {
        this.loading = true;
        this.error = false;
        const res = await this.$request({
          url: '/table/table/edit',
          method: 'GET',
          params: {
            ids: id
          }
        });
        
        if (res.code === 1) {
          this.tableInfo = res.data || {};
        } else {
          throw new Error(res.msg || '获取台桌详情失败');
        }
      } catch (error) {
        console.error('获取台桌详情失败:', error);
        this.error = true;
        this.errorMessage = error.message || '获取台桌详情失败';
      } finally {
        this.loading = false;
      }
    },
    
    // 格式化日期
    formatDate(dateString) {
      if (!dateString) return '-';
      const date = new Date(dateString);
      return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}:${date.getSeconds().toString().padStart(2, '0')}`;
    },
    
    // 编辑台桌
    handleEdit() {
      uni.navigateTo({
        url: `/pages/tabletable/components/tableedit?id=${this.tableInfo.id}`
      });
    },
    
    // 删除台桌
    handleDelete() {
      uni.showModal({
        title: '确认删除',
        content: `确定要删除台桌 "${this.tableInfo.name}" 吗？`,
        success: async (res) => {
          if (res.confirm) {
            try {
              const result = await this.$request({
                url: '/table/table/del',
                method: 'POST',
                data: {
                  ids: this.tableInfo.id
                }
              });
              
              if (result.code === 1) {
                uni.showToast({
                  title: '删除成功',
                  icon: 'success'
                });
                // 返回上一页
                setTimeout(() => {
                  uni.navigateBack();
                }, 1500);
              } else {
                throw new Error(result.msg || '删除失败');
              }
            } catch (error) {
              console.error('删除台桌失败:', error);
              uni.showToast({
                title: error.message || '删除失败',
                icon: 'none'
              });
            }
          }
        }
      });
    },
    
    // 返回上一页
    handleBack() {
      uni.navigateBack();
    }
  }
};
</script>

<style scoped>
.brand-detail-container {
  min-height: 100vh;
  background-color: #f8f8f8;
  padding: 20rpx;
}

/* 内容区 */
.content-section {
  background-color: #fff;
  border-radius: 15rpx;
  padding: 30rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

/* 基本信息 */
.basic-info {
  text-align: center;
  padding: 20rpx 0;
  border-bottom: 1rpx solid #eee;
  margin-bottom: 30rpx;
}

.detail-title {
  font-size: 36rpx;
  font-weight: bold;
  color: #333;
  display: block;
  margin-bottom: 10rpx;
}

.detail-subtitle {
  font-size: 28rpx;
  color: #666;
}

/* 详细信息列表 */
.detail-list {
  width: 100%;
}

.detail-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 25rpx 0;
  border-bottom: 1rpx solid #f0f0f0;
}

.detail-item:last-child {
  border-bottom: none;
}

.detail-label {
  font-size: 28rpx;
  color: #666;
  width: 30%;
}

.detail-value {
  font-size: 28rpx;
  color: #333;
  width: 65%;
  text-align: right;
  word-break: break-all;
}

/* 操作按钮 */
.action-buttons {
  display: flex;
  gap: 20rpx;
  margin-top: 40rpx;
}

.edit-btn {
  flex: 1;
  padding: 20rpx;
  background-color: #4cd964;
  color: #fff;
  border: none;
  border-radius: 8rpx;
  font-size: 32rpx;
}

.delete-btn {
  flex: 1;
  padding: 20rpx;
  background-color: #ff3b30;
  color: #fff;
  border: none;
  border-radius: 8rpx;
  font-size: 32rpx;
}

/* 加载状态 */
.loading-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 200rpx 0;
}

.loading-spinner {
  width: 50rpx;
  height: 50rpx;
  border: 6rpx solid #f3f3f3;
  border-top: 6rpx solid #4cd964;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 20rpx;
}

.loading-text {
  font-size: 28rpx;
  color: #666;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 错误状态 */
.error-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 200rpx 0;
}

.error-icon {
  width: 200rpx;
  height: 200rpx;
  margin-bottom: 30rpx;
  opacity: 0.5;
}

.error-text {
  font-size: 28rpx;
  color: #999;
  margin-bottom: 30rpx;
  text-align: center;
}

.retry-btn {
  padding: 15rpx 60rpx;
  background-color: #4cd964;
  color: #fff;
  border: none;
  border-radius: 8rpx;
  font-size: 28rpx;
}
</style>